<?php

namespace common\models;

use Yii;
use common\models\Shipping;
use common\models\Address;
use common\models\Region;
use common\models\OrderProduct;

/**
 * This is the model class for table "order".
 *
 * @property string $id
 * @property integer $user_id
 * @property integer $order_status
 * @property integer $shipping_status
 * @property integer $pay_status
 * @property integer $product_num
 * @property string $product_fee
 * @property integer $shipping_id
 * @property integer $address_id
 * @property string $shipping_fee
 * @property string $order_fee
 * @property string $pay_fee
 * @property string $create_time
 */
class Order extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'user_id'], 'required'],
            [['id', 'user_id', 'order_status', 'shipping_status', 'pay_status', 'product_num', 'shipping_id', 'address_id'], 'integer'],
            [['product_fee', 'shipping_fee', 'order_fee', 'pay_fee'], 'number'],
            [['message'], 'string', 'max' => 255],
            [['create_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '订单id',
            'user_id' => '用户编号',
            'order_status' => '订单状态',
            'shipping_status' => '配送状态',//' 1未配送 2配送中 3配送完成',
            'pay_status' => '支付状态',//1未支付2已支付',
            'product_num' => '商品数量',
            'product_fee' => '商品金额',
            'shipping_id' => '快递',
            'address_id' => '收货地址',
            'shipping_fee' => '配送费用',
            'order_fee' => '订单金额',
            'pay_fee' => '支付金额',
            'message' => '订单留言',
            'create_time' => '下订单时间',
        ];
    }

    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }
    public function getAddress()
    {
        return $this->hasOne(Address::className(), ['id' => 'address_id']);
    }

    public function getOrderproduct()
    {
        return $this->hasOne(OrderProduct::className(), ['order_id' => 'id']);
    }

    public static function order_product($id)
    {
       //$where['user_id'] = $user_id; 
       //$where['order_id'] = $id;
       $li = [];
       $list = OrderProduct::find()
                ->select('id,product_id')
                ->where(['order_id'=>$id])
                ->asarray()
                ->all();
                foreach ($list as $key => $value) {
                   $li[$value['id']] = $value['product_id'];
                }
                return $li;
    }
     
    
 
    public static function address_list($user_id,$address_id= '')
    {
        $li = [''=>'请先添加收货地址'];
        $where['user_id'] = $user_id;
        $where['is_show'] = 1;
 
        if($address_id){
            $where['id'] = $address_id;
        }
        $list = Address::find()
                ->select('id,province,city,district,address_detail')
                ->where($where)
                ->asarray()
                ->all();
        foreach ($list as $key => $value) {
            $v1 = Region::findOne($value['province'])->name;
            $v2 = Region::findOne($value['city'])->name;
            $v3 = Region::findOne($value['district'])->name;
            if($address_id){
                $li = $v1.' '.$v2.' '.$v3.' '.$value['address_detail'];
            }else{
                unset($li['']);
                $li[$value['id']] = $v1.' '.$v2.' '.$v3.' '.$value['address_detail'];    
            }
        }

        return $li;
    }

    public function getShipping()
    {
        return $this->hasOne(Shipping::className(), ['id' => 'shipping_id']);
    }

    public static function ship_list(){
        $list = Shipping::find()->select('shipping_name,id')->where(['status'=>1,])->asarray()->all();
        $li = [];
        foreach ($list as $key => $value) {
            $li[$value['id']] = $value['shipping_name'];
        }
        return $li;
    }

    public static function itemAlias ($type, $code = NULL)

    {
        $_items = [            
            'pay_status' => [
                //''  =>'支付状态',
                '1' => "未支付",              
                '2' => "已支付",              
            ],
            'shipping_status' => [
                //''  =>'配送状态',
                '1' => "未配送",              
                '2' => "配送中",
                '3' => "配送完成",
            ],
            'order_status' => [
                //''  =>'配送状态',
                '1' => "待付款",              
                '2' => "待发货",
                '3' => "待收货",
                '4' => "已完成",
                '5' => "已取消",
            ],
        ];

        if (isset($code))
            return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
        else
            return isset($_items[$type]) ? $_items[$type] : false;
    }

    /*
    * 保存前执行的操
    */
    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            //是否是新添加
            if($this->isNewRecord) {
                
                $this->create_time = date("Y-m-d H:i:s", time());
            }else{
                if($this->shipping_status == 2){
                    $this->order_status =3;
                }else if($this->shipping_status == 3){
                    $this->order_status =4;
                }
                $this->order_fee = $this->product_fee+$this->shipping_fee;
                $this->pay_fee = $this->product_fee+$this->shipping_fee;
            }
            return true;
        } else {
            return false;
        }
    } 
}
